package com.lonely.fx.list_view;

import javafx.application.Application;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.ListCell;
import javafx.scene.control.ListView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Font;
import javafx.stage.Stage;

/**
 * @date: 2022-07-29 15:47
 * @author: ztkj-hzb
 * @description: 鼠标悬浮放大效果演示
 **/
public class MouseHoverToZoomSettingMain extends Application {

    public static void main(String[] args) {
        launch(args);
    }

    /**
     *
     */
    @Override
    public void start(Stage primaryStage) throws Exception {

        AnchorPane anchorPane = new AnchorPane();

        ObservableList<String> list = FXCollections.observableArrayList();
        list.add("数据--A");
        list.add("数据--B");
        list.add("数据--C");
        list.add("数据--D");
        list.add("数据--E");
        list.add("数据--F");

        ListView<String> listView = new ListView<>(list);
        listView.setPrefSize(300, 300);
        //listView.setFixedCellSize(30);

        AnchorPane.setLeftAnchor(listView, 100.0);
        AnchorPane.setTopAnchor(listView, 100.0);
        anchorPane.getChildren().addAll(listView);
        primaryStage.setScene(new Scene(anchorPane));
        primaryStage.setWidth(600);
        primaryStage.setHeight(500);
        primaryStage.show();


        listView.setCellFactory(param -> {
            ListCell<String> listCell = new MyListCell();

            //鼠标移入移除时间
            listCell.hoverProperty().addListener((observable, oldValue, newValue) -> {

                if (listCell.getGraphic() == null) {
                    return;
                }

                Label graphic = (Label) listCell.getGraphic();
                int index = param.getItems().indexOf(graphic.getText());

                if (newValue) {
                    graphic.setPrefHeight(50);
                    graphic.setFont(new Font(20));
                    param.getFocusModel().focus(index);
                } else {
                    graphic.setPrefHeight(30);
                    graphic.setFont(new Font(15));
                }
            });

            return listCell;
        });


    }


    private static class MyListCell extends ListCell<String> {

        /**
         *
         */
        @Override
        protected void updateItem(String item, boolean empty) {
            super.updateItem(item, empty);
            if (empty) {
                return;
            }
            this.setGraphic(new Label(item));
        }
    }

}
